package io.wongxd.accessibility

import android.app.Activity
import android.util.Log
import io.wongxd.accessibility.utils.times
import io.wongxd.accessibility.viewnode.ViewNode
import io.wongxd.solution.logger.WLogUtil


abstract class Action {
    abstract val name: String

    /** ！！！无障碍节点在主线程操作 **/
    abstract suspend fun run(act: Activity)

    override fun toString() = name

}


/**
 * 输出布局
 */
fun printLayoutInfo(
    includeInvisible: Boolean = true,
    save2file: Boolean = false,
    ext: String = ""
) {
    ViewNode.getRoot().printWithChild(0, 0, includeInvisible, save2file, ext)
}

private fun ViewNode?.printWithChild(
    index: Int,
    dep: Int,
    includeInvisible: Boolean,
    save2file: Boolean = false,
    ext: String = ""
) {
    if (this == null) {
        if (save2file)
            WLogUtil.e(ext, "ViewNode", "*" * dep + "[$index] null")
        else
            WLogUtil.d("ViewNode", "*" * dep + "[$index] null")
        return
    }
    if (!includeInvisible && !isVisibleToUser) {
        if (save2file)
            WLogUtil.e(ext, "ViewNode", "*" * dep + "[$index] " + "InVisible")
        else
            Log.w("ViewNode", "*" * dep + "[$index] " + "InVisible")
        return
    }
    if (isVisibleToUser) {
        if (save2file)
            WLogUtil.e(ext, "ViewNode", "*" * dep + "[$index] " + toString())
        else
            Log.d("ViewNode", "*" * dep + "[$index] " + toString())
    } else {
        if (save2file)
            WLogUtil.e(ext, "ViewNode", "*" * dep + "[$index] " + toString())
        else
            Log.w("ViewNode", "*" * dep + "[$index] " + toString())
    }
    children.forEachIndexed { i, it ->
        it.printWithChild(i, dep + 1, includeInvisible)
    }
}